package com.webapp.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cgame.game.Game;
import com.cgame.game.GameFactory;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

@Controller
@RequestMapping("/admin")
public class AdminController extends AbstractController{
	protected static final String VIEW = "/admin";
	@RequestMapping("/addgame")
	@ResponseBody
	public String addgame(@RequestBody String param){
		JsonObject paramjs=new Gson().fromJson(param, JsonObject.class);
		Game game=new Game();
		game.creategame(paramjs);
		GameFactory.addGame(game);
		JsonObject result=new JsonObject();
		result.add("gamelist", GameFactory.getGameList());
		return result.toString();
	}
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(@RequestBody String sParam){
		JsonObject param=new Gson().fromJson(sParam, JsonObject.class);
		GameFactory.delete(param.get("name").getAsString());
		JsonObject result=new JsonObject();
		result.add("gamelist", GameFactory.getGameList());
		return result.toString();
	}
	@RequestMapping("/reset")
	@ResponseBody
	public String reset(@RequestBody String sParam){
		JsonObject param=new Gson().fromJson(sParam, JsonObject.class);
		Game game=GameFactory.getGame(param.get("name").getAsString());
		game.reset();
		JsonObject result=new JsonObject();
		result.add("gamelist", GameFactory.getGameList());
		return result.toString();
	}
	@RequestMapping("/gamelist")
	@ResponseBody
	public String gamelist(){
		JsonObject result=new JsonObject();
		result.add("gamelist", GameFactory.getGameList());
		return result.toString();
	}
}
